﻿using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Jungle.Code.Utils;
using Jungle.Code.Core;
using Jungle.Code.DAL;


namespace Jungle.Admin
{
	public partial class DefaultAdmin : System.Web.UI.Page
	{
        public void Page_PreInit()
        {
            this.Theme = "Admin";
        }

		private string cmd = "";
		private string nodeID = "";
        private string tnodeID = "1";
		protected void Page_Load(object sender, EventArgs e) {

            GenerateSiteMap();

            SessionKeeper thisSession = new SessionKeeper();
            thisSession.SessionId = Session.SessionID.ToString();
            thisSession.GetData();
            if ((Request["nodeID"] == null) && (thisSession.AdminLogin == "administrator"))
            {
                int langID=1;
                HttpCookie cookie = Request.Cookies["CurrLang"];
                if (cookie != null)
                {
                    langID = Convert.ToInt32(cookie["lang"]);
                    using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                    {
                        SqlCommand command = new SqlCommand("crNodes_GetTopByLangID", connect);
                        SqlDataReader reader = null;
                        SqlParameter param_langID = command.Parameters.Add("@langID", SqlDbType.Int);
                        command.CommandType = CommandType.StoredProcedure;
                        param_langID.Value = langID;


                        command.Connection.Open();
                        reader = command.ExecuteReader();
                        if (reader.Read()) tnodeID = reader.GetValue(0).ToString();
                        reader.Close();
                        command.Connection.Close();
                    }
                }
                Response.Redirect("~/Admin/Default.aspx?nodeID="+ tnodeID +"&cmd=cont");
            }
        

			if (Request["cmd"] != null)
				cmd = Request["cmd"];
			else
				cmd = "struct";

			if (Request["nodeId"] != null)
				nodeID = Request["nodeID"];

			string urlCont = UrlHelper.FormatUrl("Default.aspx", new Arg("nodeID", nodeID), new Arg("cmd", "cont"));
            string urlAdd = UrlHelper.FormatUrl("Default.aspx", new Arg("nodeID", nodeID), new Arg("cmd", "add"));
            string urlEdit = UrlHelper.FormatUrl("Default.aspx", new Arg("nodeID", nodeID), new Arg("cmd", "edit"));


			switch (cmd) {
                case "edit":
                    {
                        tabPanel.Controls.Add(Tab("Наполнение", urlCont, false));
                        tabPanel.Controls.Add(Tab("Свойства", urlEdit, true));
                        mainContainer.Controls.Add(this.Page.LoadControl("Controls/NodeEdit.ascx"));
                        break;
                    }
                case "add":
                    {
                        tabPanel.Controls.Add(Tab("Добавить страницу", urlAdd, true));
                        mainContainer.Controls.Add(this.Page.LoadControl("Controls/NodeEdit.ascx"));
                        break;
                    }
                case "cont":
                    {
                        tabPanel.Controls.Add(Tab("Наполнение", urlCont, true));
                        tabPanel.Controls.Add(Tab("Свойства", urlEdit, false));
                        mainContainer.Controls.Add(this.Page.LoadControl("Controls/ContentManager.ascx"));
                        break;
                    }
                case "mod":
                    {
                        int sectionID = 0;
                        if (Request["sectionID"] != null)
                            sectionID = int.Parse(Request["sectionID"]);
                        Section sect = new Section();
                        sect = Section.GetBySectionID(sectionID);
                        Module mod = new Module();
                        mod = Module.GetByModuleID(sect.ModuleID);
                        mainContainer.Controls.Add(this.LoadControl("~" + mod.BasePath + mod.AdminControlFile));
                        break;
                    }
               
			}
			

		}
		
		private Control Tab(string title, string url, bool active) {
			Control control1 = new Control();
			HtmlGenericControl child = new HtmlGenericControl();
			if (active) {
				child.InnerHtml = string.Concat(new string[] { "<div class=\"taba\"><div class=\"l\"><div class='r'><a href='", url, "'>", title, "</a></div></div></div>" });
			} else {
				child.InnerHtml = string.Concat(new string[] { "<div class='tab'><a href='", url, "'>", title, "</a></div>" });
			}
			new HyperLink().Text = title;
			control1.Controls.Add(child);
			return control1;
		}

        protected void GenerateSiteMap()
        {
            StreamWriter writeXml = new System.IO.StreamWriter(Server.MapPath("../sitemap.xml"), false, Encoding.UTF8);
            writeXml.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            writeXml.WriteLine("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");

            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("select * from dbo.crNodes", connect);
                SqlDataReader reader = null;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    if ((bool)reader["enabled"])
                    {
                        writeXml.WriteLine("<url>");
                        writeXml.WriteLine("<loc>" + UrlHelper.GetSiteUrl() + "/" + reader["relativeUrl"].ToString().Trim() + ".aspx</loc>");
                        writeXml.WriteLine("</url>");
                    }
                }
                reader.Close();
                command.Connection.Close();
            }

            writeXml.WriteLine("</urlset>");

            
            writeXml.Close();

            
        }
	}
}
